import com.beust.ah.A;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
       // test01();
       // test02();
//        test03();
        //test04();
      //  test05();
        //test06();

//        test07();
   // test08();
        //page04();
        test09();
    }

    private static void test09() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        webDriver.manage().window().maximize();
        sleep(1000);
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //获取当前窗口
        String target_handle = "";
        Set<String> handles = webDriver.getWindowHandles();
        for (String s:
                handles) {
            target_handle = s;
        }
        webDriver.switchTo().window( target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
        sleep(5000);
        webDriver.quit();


    }

    private static void page04() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("file:///D:/JavaCode/LeetCode/SeleniumTest/src/main/resources/test04.html");
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        //取消alert弹窗
        webDriver.switchTo().alert().dismiss();
        //确认弹窗
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        webDriver.switchTo().alert().sendKeys("hello");
        webDriver.switchTo().alert().accept();

    }

    private static void test08() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("file:///D:/JavaCode/LeetCode/SeleniumTest/src/main/resources/test03.html");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        select.selectByIndex(3);
        sleep(3000);
        select.selectByValue("8.34");

    }


    private static void test07() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        webDriver.manage().window().maximize();
        sleep(1000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(2000);
        webDriver.navigate().back();
        sleep(2000);
        webDriver.navigate().forward();
    }

    private static void test06() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(100);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(100);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");

    }

    private static void test05() throws InterruptedException, IOException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
       File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
       sleep(100);
        FileUtils.copyFile(file,new File("D:\\20231108.png"));
    }

    private static void test04() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        WebElement element = webDriver.findElement(By.cssSelector("#\\33  > div > div > div.c-row.c-gap-top-small > div.c-span3.left-image_3TJlK > div > div.image-wrapper_39wYE > div.img-mask_2AwMa"));
        Actions action = new Actions(webDriver);
        //鼠标右击
        action.moveToElement(element).contextClick().perform();
    }


    private static void test03() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        sleep(1000);
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("软件测试");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(1000);
        webDriver.manage().window().maximize();
        sleep(2000);
        webDriver.manage().window().fullscreen();
        sleep(2000);
        webDriver.switchTo().window(webDriver.getWindowHandle()).quit();
    }

    private static void test02() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
       //校验搜索结果
       // sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);
        List<WebElement> list = webDriver.findElements(By.cssSelector("a em"));
        System.out.println(list.get(1).getText());
        for (WebElement element1:list) {
            if (element1.getText().contains("测试") || element1.getText().contains("软件")){
                System.out.println("测试通过");;
            }else {
                System.out.println(element1.getText());
                System.out.println("测试不通过");
            }
        }
        webDriver.switchTo().window(webDriver.getWindowHandle()).quit();
    }

    private static void test01() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();
    }
}
